package com.framework.util;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.util.HashMap;
import java.util.Map;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

import java.io.IOException;

import org.w3c.dom.Document;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import java.io.StringReader;

import org.xml.sax.InputSource;

/**
 *
 * @author Desarrollo2
 */
public class ConvertidorXmlToMap {

	public static String convertirToXML(Map<String, Object> mapa_datos) {
		XStream magicApi = new XStream();
		String xml = magicApi.toXML(mapa_datos);
		return xml;
	}

	@SuppressWarnings("unchecked")
	public static Map<String, Object> convertirToMap(String xml_data) {
		if (xml_data != null && !xml_data.isEmpty()) {
			XStream magicApi = new XStream(new DomDriver());
			Map<String, Object> map = (Map<String, Object>) magicApi
					.fromXML(xml_data);
			return map;
		} else {
			return new HashMap<String, Object>();
		}
	}

	public static Document stringToDom(String xmlSource) throws SAXException,
			ParserConfigurationException, IOException {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();
		return builder.parse(new InputSource(new StringReader(xmlSource)));
	}

}
